<template>
	<view class="bg-white solid-bottom padding-bottom-sm" style="width: 100%;">
		<view class="flex justify-start align-start" style="width: 100%;">
			<view class="padding flex align-center justify-start" style="flex-direction: column;">
				<view @tap="jumpToUserProfile(post.author.user_id)" class="cu-avatar round"
					:style="'background-image:url('+post.author.avatar+')'">
					<image v-if="post.author.sex!==-1" class="sex-icon round" :src="'../static/images/setting/'+[post.author.sex==0?'female':'male']+'.png'"></image>
				</view>
				<view v-if="post.owner_id==circleOwnerId" class="padding-top-xs">
					<view style="max-width: 65upx;" class="radius cu-tag sm bg-cyan">圈主</view>
				</view>
				<view v-else-if="post.author.role!==null" class="padding-top-xs">
					<view style="max-width: 65upx;" :class="'radius cu-tag sm bg-'+roleColor">{{post.author.role.name}}</view>
				</view>
			</view>
			<view class="flex justify-start align-start" style="flex-direction: column;width: 100%;">
				<view class="flex justify-between align-end padding-top" style="width: 100%;">
					<view class="flex justify-start align-start" style="flex-direction: column;">
						<view class="flex justify-start align-center">
							<text @tap="jumpToUserProfile(post.owner_id)" class="text-df text-grey">{{post.author.nickname}}</text>
							<view v-if="post.author.group!==null" class="padding-left-xs">
								<view class="radius cu-tag sm bg-gray text-grey">{{post.author.group.name}}</view>
							</view>
						</view>
						<text class="text-gray text-sm">{{post.show_time}}</text>
					</view>
					<view class="flex justify-end align-center">
						<button open-type="share" :data-circle="post.circle.name" :data-author="post.author.nickname" :data-postId="post.post_id" class="cuIcon-forward text-gray padding-right cu-btn bg-white marging-right">分享</button>
						<view class="text-xl">
							<text  @tap="showExpandMenuAction($event)" class="cuIcon-more padding-right text-gray"></text>
						</view>
					</view>
				</view>
				<view v-if="post.circle_topic!==null&&showTopic" class="padding-bottom-xs padding-top">
					<view @tap.stop="clickTopic" class="flex justify-start align-center round"
						style="background-color: #eff6fb;padding: 6upx 10upx;">
						<view class="padding-right-xs">
							<image src="../static/images/setting/topic.png" style="width: 30upx;height: 30upx;">
							</image>
						</view>
						<text class="text-blue text-cut">{{post.circle_topic.title}}</text>
					</view>
				</view>
				<view class="padding-right" style="width: 100%;">
					<rich-active-display @textPress="textLongPress" :isExpand="post.is_expand" @expand="expandContent"
						@videoPlay="clickVideoPlay" :richContent="post.rich_content"></rich-active-display>
				</view>
				<!-- 来自哪个圈子 -->
				<view v-if="showCircle" class="flex justify-end align-center margin-top-xs margin-bottom-xs" style="width: 100%;">
					<view class="radius padding-left-sm padding-right">
						<text @tap="jumpToCircleProfile(post.circle_id)" class="padding-left-xs" style="color: #777777;">来自圈子《{{post.circle.name}}》</text>
					</view>
				</view>
				<!-- 评论 -->
				<view v-if="(post.comment_list&&post.comment_list.length>0)||(post.praise_list&&post.praise_list.length>0)"
					class="bg-gray radius padding-top-xs padding-bottom-sm padding-left-sm  padding-right-sm"
					style="width: 95%;">
					<!-- 点赞 -->
					<view v-if="post.praise_list&&post.praise_list.length>0"
						class="bg-gray radius margin-bottom-sm padding-sm solid-bottom" style="width: 100%;">
						<view class="flex justify-start align-center">
							<text class="cuIcon-likefill text-grey"></text>
							<view class="padding-left-xs" v-for="(item,index) in post.praise_list" :key="index">
								<view @tap="jumpToUserProfile(item.user_id)" class="cu-avatar sm" :style="'background-image: url('+item.avatar+');'"></view>
							</view>
							<text v-if="post.praise_count>post.praise_list.length"
								class="text-grey padding-left">等{{post.praise_count}}人觉得很赞</text>
							<text v-else class="text-grey padding-left">觉得很赞</text>
						</view>
					</view>
					<view v-if="post.comment_list&&post.comment_list.length>0" class="flex justify-start align-center padding-bottom-xs" style="width: 100%;">
						<text class="cuIcon-comment" style="color: #999999;"></text>
						<text style="color: #999999;">{{post.comment_count}}条评论</text>
					</view>
					<view class="flex justify-start align-start" style="flex-direction: column;width: 100%;box-sizing: border-box;">
						<view v-for="(comment,index) in post.comment_list" :key="index">
							<view class="flex flex-wrap align-center">
								<view class="flex justify-start align-center">
									<view @tap.stop="jumpToUserProfile(comment.owner_id)" class="flex justify-start align-center">
										<view v-if="comment.owner_id==post.owner_id" class="cu-tag tipLabel bg-blue">楼主</view>
										<text class="text-grey">{{comment.author.nickname}}</text>
									</view>
									<text v-if="comment.parent_comment!==null" class="cuIcon-playfill" style="color: #999999;"></text>
									<view v-if="comment.parent_comment!==null" @tap.stop="jumpToUserProfile(comment.parent_comment.owner_id)" class="flex justify-start align-center">
										<view v-if="comment.parent_comment.owner_id==post.owner_id" class="cu-tag tipLabel bg-blue">楼主</view>
										<text v-if="comment.parent_comment!==null"
											class="text-grey">{{comment.parent_comment.author.nickname}}</text>
									</view>
									<text class="text-grey">:</text>
								</view>
								<view v-if="comment.content" @longpress="copyContent(comment.content)" style="word-break:break-all;">
									<rich-text @tap="expandAction(index)" v-if="comment.content.length>0&&comment.content.length>100&&comment.is_expand==false"
										:nodes="comment.content.slice(0,99).replace(reg,emotion)">
									</rich-text>
									<text @tap="expandAction(index)" v-if="comment.content.length>0&&comment.content.length>100&&comment.is_expand==false" class="text-blue text-sm">展开</text>
									<rich-text v-if="(comment.content.length>0&&comment.content.length<100)"
										:nodes="comment.content.replace(reg,emotion)"></rich-text>
									<rich-text @tap="expandAction(index)" v-if="comment.content.length>0&&comment.content.length>100&&comment.is_expand"
											:nodes="comment.content.replace(reg,emotion)"></rich-text>
									<text @tap="expandAction(index)" v-if="comment.content.length>0&&comment.content.length>100&&comment.is_expand" class="text-blue text-sm">收起</text>
								</view>
							</view>
							<view v-if="comment.image_list&&comment.image_list.length>0" class="padding-top-xs">
								<view :style="'max-width:'+(comment.image_list.length>2?'80%;':'30%;')"
									:class="'grid flex-sub grid-square col-'+comment.image_list.length">
									<view class="bg-img" v-for="(url,index) in comment.image_list" :key="index"
										@tap="ViewImage" :data-list="comment.image_list" :data-url="url">
										<image :src="url+'?imageView2/2/w/500/h/500/q/75'" mode="aspectFill"></image>
									</view>
								</view>
							</view>
							<view v-if="comment.audio_url&&comment.audio_url.length>0"
								class="padding-top-xs padding-bottom-xs" style="width: 80%;">
								<audio-player :src="comment.audio_url" :initDuration="comment.audio_duration"
									style="width: 80%;" :autoplay="false"></audio-player>
							</view>
							<view class="padding-bottom-sm">
								<text class="padding-right" style="color: #999999;border: #999999 0.5px;">{{getShowTime(comment.created_at)}}</text>
								<text @tap="replyAction(index)" style="color: #999999;border: #999999 0.5px;">回复</text>
							</view>
						</view>
						<view v-if="post.comment_count>3&&post.comment_list.length<post.comment_count"
							class="padding-top-sm flex justify-center align-center" style="width: 100%;">
							<text @tap="loadMoreComment" class="text-blue">查看更多</text>
						</view>
					</view>
				</view>
				<view class="flex justify-between align-center padding-top-sm" style="width: 100%;">
					<view class="flex justify-start align-center text-df padding-right" style="width: 100%;">
						<view @tap="praiseAction" class="flex justify-start align-center padding-right-xl">
							<text :class="'cuIcon-appreciate text-bold '+[post.is_praise==1?'text-red':'text-gray']"></text>
							<text v-if="post.praise_count>0" :class="post.is_praise==1?'text-red':'text-gray'">{{format(post.praise_count)}}</text>
							<text v-else :class="post.is_praise==1?'text-red':'text-gray'">点赞</text>
						</view>
						<view @tap="favoriteAction" class="flex justify-start align-center padding-right-xl">
							<text :class="'cuIcon-favor text-bold '+[post.is_favorite==1?'text-red':'text-gray']"></text>
							<text v-if="post.favorite_count>0" :class="post.is_favorite==1?'text-red':'text-gray'">{{format(post.favorite_count)}}</text>
							<text v-else :class="post.is_favorite==1?'text-red':'text-gray'">收藏</text>
						</view>
						<view @tap="rewardAction" class="flex justify-start align-center text-gray">
							<image src="../static/images/setting/coin.png" style="width: 14px;height: 14px;"></image>
							<text v-if="post.reward_score>0" class="text-gray">打赏({{format(post.reward_score)}})</text>
							<text v-else class="text-gray">打赏</text>
						</view>
					</view>
					<view class="flex justify-end align-center padding-right" style="min-width: 180upx;">
						<button @tap="sendCommentAction" class="cu-btn round line-gray cuIcon-edit">评论</button>
					</view>
				</view>
			</view>
		</view>
		<chunLei-popups v-model="popMore.showExpandMenu" :x="popMore.x" :y="popMore.y" dynamic>
			<view :class="'flex align-center justify-start '+[index!==expandMenuList.length-1?'padding-bottom-sm':'']"
				style="flex-direction: column;" v-for="(item,index) in expandMenuList" :key="index">
				<view v-if="item.icon!=='share'" @tap="tapPopup(index)" class="flex justify-start align-center">
					<text :class="'padding-right-xs cuIcon-'+item.icon"></text>
					<text class="text-df">{{item.title}}</text>
				</view>
				<view v-else>
					<button style="max-width: 80upx;" open-type="share" class="bg-white text-df sm"><text class="cuIcon-share text-df"><text class="text-df padding-left-xs">分享</text></text></button>
				</view>
			</view>
		</chunLei-popups>
	</view>
</template>

<script>
	import {
		getUserInfo
	} from '@/api/auth.js'
	import RichActiveDisplay from '@/components/rich-active-display.vue';
	import AudioPlayer from '@/components/audio-player.vue';
	import chunLeiPopups from "@/components/chunLei-popups/chunLei-popups.vue";
	import {
		formatCount,
		getTagColorForRole,
		getTimeDistance
	} from '@/api/utils.js'
	import {
		isLogin
	} from '@/api/auth.js'
	import {
		checkLogin
	} from '@/js_sdk/checkLogin.js'
	export default {
		components: {
			RichActiveDisplay,
			AudioPlayer,
			chunLeiPopups
		},
		name: "ActivePostItem",
		data() {
			return {
				reg: /\#[\S]{1,3}\;/gi,
				popMore: {
					x: 30,
					y: 50,
					showExpandMenu: false,
				}
			};
		},
		props: {
			post: {
				type: Object,
				default: {}
			},
			circleOwnerId: {
				type: Number,
				default: 0,
			},
			index: {
				type: Number,
				default: -1
			},
			showTopic:{
				type:Boolean,
				default:true
			},
			showCircle:{
				type:Boolean,
				default:false
			}
		},
		computed: {
			expandMenuList() {
				let items = [
					{
						title: '举报',
						icon: 'warn'
					}
				]
				if(isLogin()) {
					let userInfo = getUserInfo()
					let isAdmin = userInfo.role!==null
					if(this.circleOwnerId==userInfo.user_id||
					this.post.owner_id==userInfo.user_id||
					isAdmin
					) {
						items.push({
							title: '删除',
							icon: 'delete'
						})
					}
				}
				return items
			},
			roleColor() {
				if (this.post == null ||
					this.post.author == null ||
					this.post.author.role == null) {
					return 'white'
				}
				return getTagColorForRole(this.post.author.role.role_id)
			}
		},
		mounted() {
			let that = this
			uni.$on('ActivePostListDidSroll',function(){
				if(that.popMore.showExpandMenu) {
					that.popMore.showExpandMenu = false
				}
			})
		},
		methods: {
			copyContent(content) 
			{
				uni.setClipboardData({
					data: content,
					success: function() {
						uni.showToast({
							icon: 'none',
							title: '内容已复制!'
						});
					}
				});
			},
			ViewImage(e) {
				console.log(e.currentTarget.dataset.list)
				console.log(e.currentTarget.dataset.url)
				uni.previewImage({
					urls: e.currentTarget.dataset.list,
					current: e.currentTarget.dataset.url
				});
			},
			expandAction(commentIndex) 
			{
				this.$emit('commentExpand',[this.index, commentIndex])
			},
			clickShareAction() 
			{
				this.$emit('onShare')
			},
			jumpToCircleProfile(circleId) 
			{
				uni.navigateTo({
					url:'/pages/lessuse/circle-profile/circle-profile?circleId='+circleId
				})
			},
			getShowTime(date) 
			{
				return getTimeDistance(date)
			},
			clickTopic()
			{
				this.$emit('clickTopic')
			},
			jumpToUserProfile(userId) {
				uni.navigateTo({
					url: '/pages/profile/profile?userId=' + userId
				})
			},
			tapPopup(index) {
				if(isLogin()==false) {
					checkLogin()
					return
				}
				if (index == 0) {
					this.$emit('report')
				} else if (index == 1) {
					this.$emit('delete')
				}
			},
			showExpandMenuAction(e) {
				this.popMore.x = e.touches[0].clientX
				this.popMore.y = e.touches[0].clientY
				this.popMore.showExpandMenu = !this.popMore.showExpandMenu
			},
			format(count) 
			{
				return formatCount(count)
			},
			praiseAction() 
			{
				if(isLogin()==false) {
					checkLogin()
					return
				}
				this.$emit('praise')
			},
			favoriteAction() 
			{
				if(isLogin()==false) {
					checkLogin()
					return
				}
				this.$emit('favorite')
			},
			rewardAction() 
			{
				if(isLogin()==false) {
					checkLogin()
					return
				}
				this.$emit('reward')
			},
			replyAction(commentIndex) {
				if(isLogin()==false) {
					checkLogin()
					return
				}
				this.$emit('reply', [this.index, commentIndex])
			},
			loadMoreComment() {
				this.$emit('loadMoreComment')
			},
			textLongPress() {
				this.$emit('textPress')
			},
			expandContent() {
				this.$emit('expand')
			},
			sendCommentAction() {
				if(isLogin()==false) {
					checkLogin()
					return
				}
				this.$emit('commentAction')
			},
			clickVideoPlay(url) {
				let destUrl = '/pages/always/video-list/video-detail?url=' + url + '&postId=' + this.post.post_id
				uni.navigateTo({
					url: destUrl
				})
			},
			emotion(res) {
				//console.log('mess', res)
				let word = res.replace(/\#|\;/gi, '')
				//console.log(word)
				const list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过',
					'酷',
					'冷汗', '抓狂', '吐', '偷笑', '可爱', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂', '疑问', '嘘',
					'晕', '折磨', '衰',
					'骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲',
					'吓', '可怜',
					'菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀',
					'足球', '瓢虫',
					'便便', '月亮', '太阳', '礼物', '拥抱', '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情',
					'飞吻', '跳跳',
					'发抖', '怄火', '转圈', '磕头', '回头', '跳绳', '挥手', '激动', '街舞', '献吻', '左太极', '右太极'
				]
				let index = list.indexOf(word)
				return `<img src="https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/${index}.gif" style="vertical-align:middle;" width="22" height="22">`
			}
		},
	}
</script>

<style lang="scss">
	.tipLabel {
		border-radius: 200upx;
		font-size: 20upx;
		padding: 0upx 10upx;
		height: 28upx;
		color: #ffffff;
	}
	
	.sex-icon {
		z-index: 10;
		position: absolute;
		bottom: -3px;
		width: 20upx;
		height: 20upx;
		background-color: #FFF;
	}
	
</style>
